package com.hs.jhzd.controller;

import com.hs.jhzd.entity.*;
import com.hs.jhzd.repository.JhFwdjxxbRepository;
import com.hs.jhzd.repository.JhJyzyglRepository;
import com.hs.jhzd.repository.JhQbfkxxbRepository;
import com.hs.jhzd.utils.ToolUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
集体教育资源管理
图像辨认
 */
@Controller
@RequestMapping("/acp-cloud/other")
@Slf4j
public class JhJyzyglController {
    @Value("${jsbh}")
    private String jsbh;
    @Value("${jsmc}")
    private String jsmc;
    @Autowired
    private JhJyzyglRepository repository;
    @Autowired
    private JhFwdjxxbRepository jhFwdjxxbRepository;
    @Autowired
    private JhQbfkxxbRepository jhQbfkxxbRepository;

    @RequestMapping("/jyzy/list")
    @ResponseBody
    //获取法律法规
    public List<VideoPlay> findJyzy(String type) throws SQLException, UnsupportedEncodingException {
        List<VideoPlay> videoPlays = new ArrayList<>();
        List<JhJyzyglEntity> byType = repository.findByType(type);
        for (JhJyzyglEntity jhJyzyglEntity : byType) {
            VideoPlay videoPlay = new VideoPlay();
            videoPlay.setAddress("/acp-cloud" + jhJyzyglEntity.getUrl());
            videoPlay.setCreateTime(jhJyzyglEntity.getSjc());
            videoPlay.setImgUrl("/acp-cloud" + jhJyzyglEntity.getTplj());
            videoPlay.setMc(jhJyzyglEntity.getMc());
            videoPlay.setJsbh(jhJyzyglEntity.getJsbh());
            videoPlay.setZyfl(jhJyzyglEntity.getZyfl());
            videoPlay.setLx(jhJyzyglEntity.getLx());
            if (jhJyzyglEntity.getNr() != null) {
                Blob nr = jhJyzyglEntity.getNr();
                String blobString = new String(nr.getBytes(1, (int) nr.length()), "GBK");
                videoPlay.setNr(blobString);
            }
            videoPlays.add(videoPlay);

        }
        return videoPlays;
    }

    @RequestMapping("/qbfk/save")
    @ResponseBody
    public boolean saveqbfk(JhQbfkxxbEntity jhQbfkxxbEntity) {
        try {
            //生成主键--------

            String oldId = repository.findZybh();
            jhQbfkxxbEntity.setZybh(ToolUtils.CreatePrimaryKey(oldId));
            jhQbfkxxbEntity.setClbj("0");
            jhQbfkxxbEntity.setCjsj(new Date());
            jhQbfkxxbEntity.setScbj(false);
            jhQbfkxxbEntity.setJsbh(jsbh);
            jhQbfkxxbEntity.setJsmc(jsmc);
            jhQbfkxxbEntity.setSjc(new Date());
            jhQbfkxxbRepository.save(jhQbfkxxbEntity);
        } catch (Exception e) {
            log.error("新增异常", e);
            return false;
        }
        return true;
    }

    @RequestMapping("/fwdj/list")
    @ResponseBody
    public List<JhFwdjxxbDto> findfwdj() throws SQLException, UnsupportedEncodingException {
        List<JhFwdjxxbEntity> all = jhFwdjxxbRepository.findAll();
        List<JhFwdjxxbDto> result = new ArrayList<>();
        for (int i = 0; i < all.size(); i++) {
            JhFwdjxxbEntity jhFwdjxxbEntity = all.get(i);
            JhFwdjxxbDto dto = new JhFwdjxxbDto();
            BeanUtils.copyProperties(jhFwdjxxbEntity, dto);
            dto.setVurl("/acp-cloud"+jhFwdjxxbEntity.getVurl());
            dto.setUrl("/acp-cloud"+jhFwdjxxbEntity.getUrl());
            if (jhFwdjxxbEntity.getNr() != null) {
                Blob nr = jhFwdjxxbEntity.getNr();
                String blobString = new String(nr.getBytes(1, (int) nr.length()), "GBK");
                dto.setNr(blobString);
            }
            result.add(dto);
        }
        return result;
    }
}
